#include <stdio.h>
#include <windows.h>
#include <Commctrl.h>
#include "exp_api.h"
#include "resource.h"
#include "exjpeg.h"

extern int RegTinyGet(HKEY tree, const char *key_name, const char *val_name,
				  LPVOID buffer);
extern int RegTinySet(HKEY tree, const char *key_name, const char *val_name,
				  DWORD type, LPVOID buffer, DWORD length);
void __fastcall SetDefaultOptions(void);
void __fastcall LoadOptions(void);

HINSTANCE ghDLLInst;
jpeg_opt exp_opt; //オプション
//コンボボックス
LPCTSTR combostr[] = {"4：1：1", "4：2：2", "4：4：4",};

//---------------------------------------------------------------------------
int WINAPI DllMain(HINSTANCE hinst, unsigned long reason, void*lpvReserved)
{
    switch (reason) {
        case DLL_PROCESS_ATTACH:
			ghDLLInst = hinst;
            LoadOptions();
            break;
		case DLL_PROCESS_DETACH:
			break;
    }

    return 1;
}
//---------------------------------------------------------------------------
void __fastcall LoadOptions(void)
{
//デフォルト設定
    //圧縮率
    exp_opt.qual = 75;
    //プログレッシブ
    exp_opt.prog = 1; //プログレッシブ
    //コメントを埋めこむ
    exp_opt.comment = 0; //false
    //拡張子は".jpeg"
    exp_opt.extjpeg = 0; //false
	//サブサンプリング
	exp_opt.subsampl = IJL_411; //

//レジストリから読み込み
    //圧縮率
	RegTinyGet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
				"quality", &exp_opt.qual);
    //プログレッシブ
	RegTinyGet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
				"progressive", &exp_opt.prog);
    //コメント
	RegTinyGet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
				"comment", &exp_opt.comment);
    //拡張子は".jpeg"
	RegTinyGet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
				"extjpeg", &exp_opt.extjpeg);
    //サブサンプリング
	RegTinyGet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
				"subsampl", &exp_opt.subsampl);

	//コンボボックスの番号
	switch (exp_opt.subsampl) {
		case IJL_NONE:
			exp_opt.subcompno = 2;
			break;
		case IJL_422:
			exp_opt.subcompno = 1;
			break;
		default:
			exp_opt.subcompno = 0;
	}

}
//---------------------------------------------------------------------------
//
BOOL CALLBACK
setting_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	char qstr[4];
	LRESULT tqual;
	switch ( msg ){
		case WM_INITDIALOG:
			sprintf(qstr, "%d", exp_opt.qual);
			SetWindowText(GetDlgItem(hwnd, QUALITY), qstr);
			//SetRangeMin
			SendMessage(GetDlgItem(hwnd, QUAL_SLIDER), TBM_SETRANGEMIN, FALSE, 1);
			//SetRangeMax
			SendMessage(GetDlgItem(hwnd, QUAL_SLIDER), TBM_SETRANGEMAX, FALSE, 100);
			//SetTicFreq
			SendMessage(GetDlgItem(hwnd, QUAL_SLIDER), TBM_SETTICFREQ, 10, 0L);
			//SetPos
			SendMessage(GetDlgItem(hwnd, QUAL_SLIDER), TBM_SETPOS, TRUE, exp_opt.qual);
			if (exp_opt.prog)
				CheckDlgButton(hwnd, PROG_CHECK, BST_CHECKED);
			if (exp_opt.comment)
				CheckDlgButton(hwnd, COM_CHECK, BST_CHECKED);
			if (exp_opt.extjpeg)
				CheckDlgButton(hwnd, EXT_CHECK, BST_CHECKED);
			SendMessage(GetDlgItem(hwnd, YCrCb_COMBO), CB_INSERTSTRING, (WPARAM)0, (LPARAM)combostr[0]);
			SendMessage(GetDlgItem(hwnd, YCrCb_COMBO), CB_INSERTSTRING, (WPARAM)1, (LPARAM)combostr[1]);
			SendMessage(GetDlgItem(hwnd, YCrCb_COMBO), CB_INSERTSTRING, (WPARAM)2, (LPARAM)combostr[2]);
			SendMessage(GetDlgItem(hwnd, YCrCb_COMBO), CB_SETCURSEL, (WPARAM)exp_opt.subcompno, 0L);
   			break;
		case WM_DESTROY:
			break;
		case WM_NOTIFY:
			switch ( (int)(wParam) ) {
				case QUAL_SLIDER:
					//GetPos
					tqual = SendMessage(GetDlgItem(hwnd, QUAL_SLIDER), TBM_GETPOS, 0, 0l);
					sprintf(qstr, "%d", tqual);
					SetWindowText(GetDlgItem(hwnd, QUALITY), qstr);
					break;
			}
			break;
		case WM_COMMAND:
			switch ( LOWORD(wParam) ) {
				case IDOK:
					exp_opt.qual = SendMessage(GetDlgItem(hwnd, QUAL_SLIDER), TBM_GETPOS, 0, 0l);
					exp_opt.prog = IsDlgButtonChecked(hwnd, PROG_CHECK);
					exp_opt.comment = IsDlgButtonChecked(hwnd, COM_CHECK);
					exp_opt.extjpeg = IsDlgButtonChecked(hwnd, EXT_CHECK);
					exp_opt.subcompno = SendMessage(GetDlgItem(hwnd, YCrCb_COMBO), CB_GETCURSEL, 0L, 0L);
    				EndDialog(hwnd, IDOK);
					return TRUE;
				case IDCANCEL:
					EndDialog(hwnd, IDCANCEL);
					return TRUE;
			}
			break;
		default:
			;
	}
	return FALSE;
}
//---------------------------------------------------------------------------
int __stdcall ConfigurationDlg(HWND parent, int fnc)
{
#define XPI_CFGDLG_ABOUT    0
#define	XPI_CFGDLG_SETTING  1
#define	XPI_CFGDLG_RESERVED 2

	int ret;
    switch (fnc) {
        case XPI_CFGDLG_SETTING:
			ret = DialogBox(ghDLLInst, MAKEINTRESOURCE(SET_DIALOG), parent, setting_proc);
			if (ret == IDOK) {
				//レジストリに保存
				//圧縮率
				RegTinySet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
							"quality", REG_DWORD, &exp_opt.qual, sizeof(exp_opt.qual));
			    //プログレッシブ
				RegTinySet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
							"progressive", REG_DWORD, &exp_opt.prog, sizeof(exp_opt.prog));
			    //コメント
				RegTinySet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
							"comment", REG_DWORD, &exp_opt.comment, sizeof(exp_opt.comment));
			    //拡張子
				RegTinySet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
							"extjpeg", REG_DWORD, &exp_opt.extjpeg, sizeof(exp_opt.extjpeg));
			    //サブサンプリング
				switch (exp_opt.subcompno) {
					case 0:
						exp_opt.subsampl = IJL_411;
						break;
					case 1:
						exp_opt.subsampl = IJL_422;
						break;
					case 2:
						exp_opt.subsampl = IJL_NONE;
						break;
				}
				RegTinySet(HKEY_CURRENT_USER, "Software\\shimitei\\exjpeg", 
							"subsampl", REG_DWORD, &exp_opt.subsampl, sizeof(exp_opt.subsampl));
			}
			return XPI_ALL_RIGHT;
    }
    return XPI_NO_FUNCTION;
}
//---------------------------------------------------------------------------
